package com.my.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.entity.Music;
import com.my.admin.service.MusicService;
import com.my.admin.util.FastDFSUtil;
import com.my.admin.util.FileUploadUtil;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class MusicController {


    @Autowired
    private MusicService service;

    @Value("${fdfs.web-server-url}")
    private String webServerUrl;

    public final static List<String> IMG_FORMAT = new ArrayList<String>(){{
        add("JPG");add("JPEG");add("PNG");add("GIF");
    }};

    public final static List<String> MUSIC_Format = new ArrayList<String>(){{
        add("WAV");add("MP3");add("WMA");add("MPEG");add("ACC");add("FLAC");add("OGG");
    }};

    public final static List<String> LYRIC_Format = new ArrayList<String>(){{
        add("LRC");add("ID3");add("TXT");add("TAG");add("SRT");add("UTF");add("SMI");
    }};

    @Autowired
    private FastDFSUtil dfsUtil;

    @PostMapping("music/get")
    public Rest images(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody Music music) {
        return Rest.ok().setData("page", service.page(new Page<>(pageNo, pageSize), new QueryWrapper<Music>().
                        like(StrUtil.isNotBlank(music.getName()), "name", music.getName()).
                        like(StrUtil.isNotBlank(music.getSinger()), "singer", music.getSinger()).
                        eq(music.getSingerSex() != null, "singer_sex", music.getSingerSex())));
    }

    @PostMapping("musics")
    public Rest add(@RequestBody Music music) {
        if(service.save(music)) {
            return Rest.ok("添加成功！");
        }
        return Rest.err("添加失败!");
    }

    @PutMapping("musics")
    public Rest edit(@RequestBody Music music) {
        if(service.updateById(music)) {
            return Rest.ok("修改成功！");
        }
        return Rest.err("修改失败！");
    }

    @DeleteMapping("musics")
    public Rest del(@RequestParam long id) {
        Music music = service.getById(id);
        try {
            if(StrUtil.isNotBlank(music.getSinger())) {
                dfsUtil.delete(music.getSinger());
            }
            if(StrUtil.isNotBlank(music.getCover())) {
                dfsUtil.delete(music.getCover());
            }
            if(StrUtil.isNotBlank(music.getUrl())) {
                dfsUtil.delete(music.getUrl());
            }
            if(StrUtil.isNotBlank(music.getLyricUrl())) {
                dfsUtil.delete(music.getLyricUrl());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        if(service.removeById(id)) {
            return Rest.ok("删除成功！");
        }
        return Rest.err("删除失败！");
    }

    @PostMapping("music/imageUpload")
    public Rest imageUpload(@RequestParam("file") MultipartFile file) {
        return FileUploadUtil.verify(file, IMG_FORMAT, webServerUrl, dfsUtil);
    }

    @PostMapping("music/musicUpload")
    public Rest musicUpload(@RequestParam("file") MultipartFile file) {
        return FileUploadUtil.verify(file, MUSIC_Format, webServerUrl, dfsUtil);
    }

    @PostMapping("music/lyricUpload")
    public Rest lyricUpload(@RequestParam("file") MultipartFile file) {
        return FileUploadUtil.verify(file, LYRIC_Format, webServerUrl, dfsUtil);
    }

    @DeleteMapping("music/delete")
    public Rest delete(@RequestParam(defaultValue = "") String path) {
        if(StrUtil.isNotBlank(path)) {
            dfsUtil.delete(path);
        }
        return Rest.ok("文件清理成功！");
    }

}
